<?php

namespace app\api\model;

use think\Model;

class OrderProduct extends Model
{
    /**
     * 根据订单号获取订单商品
     * @param $orderId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOrderProductByOrderId($orderId)
    {
        return self::where('order_id', '=', $orderId)
            ->select()->toArray();
    }


    /**
     * 创建订单 - 商品中间表，记录所需数据
     * @param $order_id
     * @param $orderProducts
     * @return \think\Collection
     * @throws \Exception
     */
    public function createOrderUnionProduct($order_id, $orderProducts)
    {
        foreach ($orderProducts as &$one)
        {
            $one['order_id'] = $order_id;
        }
        return self::saveAll($orderProducts);
    }
}
